<?php
/**
 * Created by PhpStorm.
 * User: Auser
 * Date: 2017/1/4
 * Time: 17:29
 */

namespace App\Repositories\Xjb;
use Cache;
use App\Models\Xjb\CancerXjbItem;
use App\Repositories\Repository;
class CancerXjbItemRepository extends Repository
{
    static $tag = 'cancer.Xjb.item';

    //获取题目列表
    public function getItem($tid){
        $key = getCacheKey('testing.cache_key.cancer.xjb.item');
        $items = $this ->remember($key,function()use($tid){
            $item = CancerXjbItem::select('index','title','question_type as questionType','options')
                ->where('testing_id',$tid)
                ->orderBy('index','asc')
                ->get();
            foreach($item as $k=>$v){
                $item[$k]['options'] = json_decode($v['options'],true);
            }
            $data['questionList']=$item;
            return $data;
        });
        return $items;
    }

    /**根据题号获取映射关系
     * @param $index
     */
    public function getRelation($index){
        $key = getCacheKey('testing.cache_key.cancer.xjb.item.relation');
        $key = $key.$index;
        $res = $this ->remember($key,function()use($index){
            $item = CancerXjbItem::select('relation')
                ->where('index',$index)
                ->first()
                ->toarray();
            if($item){
                return $item['relation'];
            }else{
                return null;
            }
        });
        return $res;
    }

    public function model()
    {
        return app(CancerXjbItem::class);
    }

    public function tag()
    {
        return self::$tag;
    }

}